import * as Constant from '../../utils/constant'
// import commonapi from '../../api/common/common'
// initial state
const state = {
  nowDate: null,
  backdrop: false,
  isCollapse: false,
  uniqueOpened: true,
  columnSelect: false,
  queryCollapseStatus: false,
  bellCollapseStatus: false,
  queryCollapseIcon: 'el-icon-arrow-down',
  toolHeaderClass: 'el-icon-setting',
  toolHeaderStyle: 'color:#409eff;font-size: 25px;',
  tableHeaderBGC: 'rgba(138, 138, 138, 0.25)', // #f2f2f2 #99999924
  curIndexName: [],
  // tableColumns: [],
  tableColumns: Constant.TABLE_COLUMNS,
  changeTableColumns: [],
  currentTableColumns: null,
  pagingPageSizes: Constant.VUE_PAGING_DEFAULT_PAGESIZES, // pagingPageSizes: 分页的每页显示条数列表，注意：该值初始化之后不要改变
  /**
   * pagingPageSizeInit: 分页的每页显示条数，初始化之后不再改变。
   * 注意：该值用于初始化分页插件，后续查询数据使用pagingPageSize属性；
   * 因为测试发现初始化，后续改变都使用pagingPageSize，偶尔会出现发起两次查询，并不能百分百测试出来，所以做一个分开。
   */
  pagingPageSizeInit: Constant.VUE_PAGING_DEFAULT_PAGESIZE,

  projectItem: null,//新增项目回退带参数
}

// getters
const getters = {
  nowDate: state => {
    return state.nowDate
  },
  visableColumns: state => {
    // return state.tableColumns.filter((item) => { return ((item.type & 1) !== 0 && (item.status === null || (item.status & 1))) })
    let columns = {}
    for (var key in state.tableColumns) {
      // let newa = state.tableColumns[key].filter((item) => { return ((item.type & 1) !== 0 && (item.status === null || (item.status & 1))) })
      let newa = state.tableColumns[key].filter((item) => { return ((item.type & 1) !== 0 && (item.status === null || (item.status & 1))) })
      columns[key] = newa
    }
    return columns
  }
}

// actions
const actions = {
  changeIndexName ({ commit }, data) {
    if (data && data.length > 0) {
      commit('changeIndexNameByRouter', data)
    }
  },
  getNowDate ({ commit }) {
    var reqData = null
    commonapi.getNowDate(response => {
      if (response && response.status === Constant.RESP_STATE_SUCCESS) {
        var responseData = response.data

        if (responseData && responseData.rtcode === '000') {
          commit('setNowDate', responseData.data)
        }
      }
    }, reqData)
  },
  changeBackdrop ({ commit }) {
    commit(Constant.BACKDROP_STATUS_CHANGED)
  },
  handleCollapse ({ commit }) {
    commit(Constant.COLLAPSE_STATUS_CHANGED)
  },
  changeColumnSel ({ commit }, data) {
    // 改变 editTableColumns exportColumns
    commit('setChangeTableColumns', data)
    commit(Constant.BACKDROP_STATUS_CHANGED)
    commit(Constant.COLUMN_STATUS_CHANGED)
  },
  queryCollapse ({ commit }) {
    commit(Constant.QUERY_COLLAPSE_CHANGED)
  },
  toggleBellMsg ({ commit }) {
    commit(Constant.BELL_COLLAPSE_CHANGED)
    commit(Constant.BACKDROP_STATUS_CHANGED)
  },
  getColumnsByUser ({ commit }, data) {
    // commit('PRE_COLUMNS_CHANGED', data)
    // commonapi.getColumnsByUser(response => {
    //   commit(Constant.COLUMNS_CHANGED, {resp: response, tablecode: data})
    // }, data)
  },
  saveUserColumns ({ state, commit }, data) {
    // commonapi.saveUserColumns(response => {
    //   commit(Constant.COLUMNS_CHANGED, {resp: response, tablecode: state.currentTableColumns})
    // }, data)
  },

  saveProjectItem ({ commit }, data) {
    console.log("saveProjectItem",data)
    commit('setProjectItem', data)
  }
}

// mutations
const mutations = {
  changeIndexNameByRouter (state, names) {
    state.curIndexName = names
  },
  setNowDate (state, nowDateSet) {
    state.nowDate = nowDateSet
  },
  BACKDROP_STATUS_CHANGED (state) {
    state.backdrop = !state.backdrop
    if (!state.backdrop && state.columnSelect) {
      state.columnSelect = !state.columnSelect
    }
    if (!state.backdrop && state.bellCollapseStatus) {
      state.bellCollapseStatus = !state.bellCollapseStatus
    }
  },
  COLLAPSE_STATUS_CHANGED (state) {
    state.isCollapse = !state.isCollapse
  },
  COLUMN_STATUS_CHANGED (state) {
    state.columnSelect = !state.columnSelect
  },
  QUERY_COLLAPSE_CHANGED (state) {
    state.queryCollapseStatus = !state.queryCollapseStatus
    if (state.queryCollapseIcon === 'el-icon-arrow-down') {
      state.queryCollapseIcon = 'el-icon-arrow-up'
    } else {
      state.queryCollapseIcon = 'el-icon-arrow-down'
    }
  },
  BELL_COLLAPSE_CHANGED (state) {
    state.bellCollapseStatus = !state.bellCollapseStatus
  },
  COLUMNS_CHANGED (state, data) {
    let response = data.resp
    let tablecode = data.tablecode
    if (response.data && response.data.rtcode === Constant.RT_SUCCESS) {
      state.tableColumns[tablecode] = response.data.data
    }
  },
  setChangeTableColumns (state, data) {
    state.changeTableColumns = state.tableColumns[data]
    state.currentTableColumns = data
  },

  setProjectItem (state, data) {
    state.projectItem = data
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
